package com.jishuo.base.bean;

import lombok.Data;

import java.util.List;

/**
 * 基础返回结果对象
 *
 * @author jhb
 */
@Data
public class BaseResult {
    // 返回代码
    private String code;
    // 提示信息
    private String msg;
    // 其他信息
    private String msgExt;
    // 是否成功
    private boolean success;
    // 数据对象
    private Object data;
    // 数据列表
    private List<Object> dataList;

    /**
     * 默认成功
     */
    public BaseResult() {
        this.code = ErrorCodeEnum.SUCCESS.getCode();
        this.msg = ErrorCodeEnum.SUCCESS.getMsg();
        this.success = true;
    }

    /**
     * 返回成功
     * 自定义data
     */
    public BaseResult(Object data) {
        this.code = ErrorCodeEnum.SUCCESS.getCode();
        this.msg = ErrorCodeEnum.SUCCESS.getMsg();
        this.success = true;
        this.data=data;
    }


    /**
     * 自定义返回码
     * @param code 返回码
     * @param msg 提示信息
     */
    public BaseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.success = ErrorCodeEnum.SUCCESS.getCode().equals(this.code);
    }

    /**
     * 用错误代码定义
     * @param err 错误代码
     */
    public BaseResult(ErrorCodeEnum err) {
        this.code = err.getCode();
        this.msg = err.getMsg();
        this.success = ErrorCodeEnum.SUCCESS.getCode().equals(this.code);
    }

    /**
     * 用错误代码定义
     * @param err 错误代码
     */
    public BaseResult(ErrorCodeEnum err,Object data) {
        this.code = err.getCode();
        this.msg = err.getMsg();
        this.success = ErrorCodeEnum.SUCCESS.getCode().equals(this.code);
        this.data=data;
    }

    public void setCode(String code) {
        this.code = code;
        this.success = ErrorCodeEnum.SUCCESS.getCode().equals(this.code);
    }

    public static BaseResult CustomErr(String msg) {
        return new BaseResult(ErrorCodeEnum.CUSTOM_ERR.getCode(), msg);
    }
}
